import { connection } from '../app/database/mysql';
import { getIdKitap } from '../kitap/kitap.service';
import { getUserById } from '../user/user.service';
import { commentModel } from './comment.model';

/**
 * 获取内容
 */
export const getComment = async (book_id: number, pageNo: number, pageSize: number, status: number) => {
  // 准备查询
  let statement = `
    SELECT SQL_CALC_FOUND_ROWS *, COUNT(id) OVER () AS total FROM comment
  `;
  let where = []
  if (book_id > 0) {
    where.push(`book_id = ` + book_id)
  }if (status) {
    where.push(`status = ` + status)
  }
  if (where.length > 0) {
    statement += ` where `
    for (let i = 0; i < where.length; i++) {
      statement += where[i]
      if (i < where.length - 1) {
        statement += ` and `
      }
    }
  }
  statement += ` ORDER by id DESC, id DESC limit ` + (pageNo > 0 ? (pageNo - 1) * pageSize : 0) + `,` + pageSize

  // 执行查询
  const [data] = await connection.promise().query(statement);

  let totalCount: any;
  if (data[0] && data[0].total) totalCount = data[0].total;
  let totalPage = (totalCount % pageSize) > 0 ? parseInt(`${totalCount / pageSize}`) + 1 : totalCount / pageSize;

  for(let item of data as any) {
    item.user_info = await getUserById(item.user_id);
    delete item.user_info.openId;

    item.kitapName = await getIdKitap(item.book_id);
  }

  let res = {
    data,
    totalPage,
    totalCount
  }
  
  // 提供数据
  return res;
};

/**
 * 按 ID 获取内容
 */
export const getIdComment = async (postId: number) => {
  // 准备查询
  const statement = `
    SELECT * FROM comment
    WHERE id = ?
  `;

  // 执行查询
  const [data] = await connection.promise().query(statement, postId);

  // 提供数据
  return data;
}


/**
 * 创建内容
 */
export const createComment = async (post: commentModel) => {
  // 准备查询
  const statement = `
    INSERT INTO comment
    SET ?
  `;

  // 执行查询
  const [data] = await connection.promise().query(statement, post);

  // 提供数据
  return data;
}

/**
 * 更新评论
 */
export const updateComment = async (post: commentModel, postId: number) => {
  // 准备查询
  const statement = `
    UPDATE comment
    SET ?
    WHERE id = ?
  `;

  // 执行查询
  const [data] = await connection.promise().query(statement, [post, postId]);

  // 提供数据
  return data;
}

/**
 * 创建点赞
 */
export const setLikeComments = async (post: commentModel) => {
  // 准备查询
  const statement = `
    INSERT INTO comment_likes
    SET ?
  `;

  // 执行查询
  const [data] = await connection.promise().query(statement, post);

  // 提供数据
  return data;
}

/**
 * 获取用户评论点赞
 */
export const getComment_likes = async (commentId: number, userId: number) => {
  // 获取点赞数
  const statement = `
    SELECT * FROM comment_likes
    WHERE comment_id = ? AND user_id = ?
  `;

  // 执行查询
  const [data] = await connection.promise().query(statement, [commentId, userId]);

  // 提供数据
  return data;
}

/**
 * 删除内容
 */
export const deleteComment = async (post: any, postId: number) => {
  // 准备查询
  // const statement = `
  //   DELETE FROM comment
  //   WHERE id = ?
  // `;

  const statement = `
    UPDATE comment
    SET ?
    WHERE id = ?
  `;

  // 执行查询
  const [data] = await connection.promise().query(statement, [post, postId]);

  // 提供数据
  return data;
}

/**
 * 删除点赞评论
 */
export const deleteLikeComments = async (postId: number) => {
  // 准备查询
  const statement = `
    DELETE FROM comment_likes
    WHERE id = ?
  `;

  // 执行查询
  const [data] = await connection.promise().query(statement, postId);

  // 提供数据
  return data;
}

/** 
 * 获取用户点赞评论
 */
export const getUserLikeComments = async (userId: number) => {
  // 准备查询
  const statement = `
    SELECT * FROM comment_likes
    WHERE user_id = ?
  `;

  // 执行查询
  const [data] = await connection.promise().query(statement, userId);

  // 提供数据
  return data;
}
